import urlpyroxy

SERVERTIME = 0

def getBinance(api):
    if api[:4] != 'http' and api[0] != '/':
        api = '/' + api
    if api[:4] == '/fapi':  #U本位api
        api = 'https://fapi.binance.com' + api
    elif api[:4] == '/dpi': #币本位api
        api = 'https://dapi.binance.com' + api
    elif api.find('pair=') != -1: #币本位api
        api = 'https://dapi.binance.com' + api
    elif api.find('symbol=') != -1: #币本位api
        api = 'https://dapi.binance.com' + api
    # url = 'http://hockshop.xyz:8080' + api
    data = urlpyroxy.getUrl(api)
    return data
#U本位币安永续合约资金费率
def getURate(api = 'fapi/v1/premiumIndex?symbol=BTCUSDT' ): 
    data = getBinance(api)
    if data:
        funding_rate = data.get('lastFundingRate')
        return funding_rate
    return None
#币本位币安永续合约资金费率
def getBRate(api = 'dapi/v1/premiumIndex?symbol=BTCUSD_PERP' ): 
    data = getBinance(api)
    if data:
        funding_rate = data.get('lastFundingRate')
        return funding_rate
    return None
#获取币安B本位大仓前20%多空人数比和变化百分比
def getMoreBV(api = 'futures/data/topLongShortPositionRatio',pair = 'BTCUSD',period = '30m',limit = 5): 
    api = api + '?pair=%s&period=%s&limit=%d'%(pair,period,limit)
    data = getBinance(api)
    if data:
        last = data[-1]
        last2 = data[-2]
        bv = last.get('longShortRatio')
        bv2 = last2.get('longShortRatio')
        bx = 100*(bv - bv2)/bv2
        return bv,'%.3f%%'%(bx)
    return None,None
#获取币安U本位大仓前20%多空人数比
def getMoreUV(api = 'futures/data/topLongShortPositionRatio',symbol = 'BTCUSDT',period = '30m',limit = 5):
    api = api + '?symbol=%s&period=%s&limit=%d'%(symbol,period,limit)
    data = getBinance(api)
    if data:
        last = data[-1]
        last2 = data[-2]
        bv = last.get('longShortRatio')
        bv2 = last2.get('longShortRatio')
        bx = 100*(bv - bv2)/bv2
        return bv,'%.3f%%'%(bx)
    return None,None

#获取币安B本位大仓多空人数比和变化百分比/futures/data/globalLongShortAccountRatio
def getMoreBP(api = 'futures/data/globalLongShortAccountRatio',pair = 'BTCUSD',period = '30m',limit = 5): 
    api = api + '?pair=%s&period=%s&limit=%d'%(pair,period,limit)
    data = getBinance(api)
    if data:
        last = data[-1]
        last2 = data[-2]
        bv = last.get('longShortRatio')
        bv2 = last2.get('longShortRatio')
        bx = 100*(bv - bv2)/bv2
        return bv,'%.3f%%'%(bx)
    return None,None
#获取币安U本位大仓多空人数比和变化百分比,这个可以和okx的比较
def getMoreUP(api = 'futures/data/globalLongShortAccountRatio',symbol = 'BTCUSDT',period = '30m',limit = 5):
    api = api + '?symbol=%s&period=%s&limit=%d'%(symbol,period,limit)
    data = getBinance(api)
    if data:
        last = data[-1]
        last2 = data[-2]
        bv = last.get('longShortRatio')
        bv2 = last2.get('longShortRatio')
        bx = 100*(bv - bv2)/bv2
        return bv,'%.3f%%'%(bx)
    return None,None
#获取币安服务器时间
def getTime(api = 'fapi/v1/time'):
    global SERVERTIME
    data = getBinance(api)
    if data:
        sertime = int(data.get('serverTime')/1000) #单位秒
        if sertime:
            SERVERTIME = sertime
            return int(sertime)
    return 0